#include "iostream"
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
 };

ListNode* getKthFromEnd(ListNode* head, int k) {
    ListNode *p=head;
    int n=0;
    while (p!=NULL){
        p=p->next;
        n++;
    }
    p=head;
    for (int i = 0; i < n-k; ++i) {
        p=p->next;
    }
    head=p;
    return head;
}